﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Net;


namespace CCPSIP
{
    /// <summary>API_068(現在未使用)：通訊錄程式自動維護PHONE_HQ資料表API</summary>

    public class phone_hq_auto : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";

            string ApiPassword = "27466a91-1895-4a6e-8a5b-554fc90a61a8";
            /*
            try
            {
                //【01】取得POST參數
                string Password = req.Form["Password"];
                string Phone_Num = req.Form["Phone_Num"];
                string PPS_Code = req.Form["PPS_Code"];
                string Full_Name = req.Form["Full_Name"];
                string Section_Name = req.Form["Section_Name"];
                string Operation = req.Form["Operation"];

                //檢查密碼
                if (Password != ApiPassword)
                    throw new Exception("Error_01: Password error.");  //密碼錯誤

                //檢查參數
                if (string.IsNullOrEmpty(Operation))
                {
                    throw new Exception("Error_02: Parameter error.");  //參數錯誤
                }
                else if (Operation == OracleDataMaintainKit.Operation.Delete.ToString())    //Delete
                {
                    if (string.IsNullOrEmpty(PPS_Code))
                        throw new Exception("Error_02: Parameter error.");  //參數錯誤
                }
                else    //Insert, Update
                {
                    if (string.IsNullOrEmpty(Phone_Num) || string.IsNullOrEmpty(PPS_Code) || string.IsNullOrEmpty(Full_Name)
                    || string.IsNullOrEmpty(Section_Name))
                        throw new Exception("Error_02: Parameter error.");  //參數錯誤
                }

                //【02】操作Oracle資料表
                OracleDataMaintainKit.PhoneHQ PersonData = new OracleDataMaintainKit.PhoneHQ(Phone_Num, PPS_Code, Full_Name, Section_Name);

                if (Operation == OracleDataMaintainKit.Operation.Insert.ToString())
                {
                    //【02-1】新增台北公司人員資料
                    if (!OracleDataMaintainKit.CheckPhoneHQ(PPS_Code))
                    {
                        if (!OracleDataMaintainKit.InsertPhoneHQ(PersonData))
                            throw new Exception("Error_03: Insert failed.");
                    }
                    else
                        throw new Exception("Error_03: The person is already exist in PhoneHQ table.");
                }
                else if (Operation == OracleDataMaintainKit.Operation.Update.ToString())
                {
                    //【02-2】更新台北公司人員資料
                    if (OracleDataMaintainKit.CheckPhoneHQ(PPS_Code))
                    {
                        if (!OracleDataMaintainKit.UpdatePhoneHQ(PersonData))
                            throw new Exception("Error_04: Update failed.");
                    }
                    else
                        throw new Exception("Error04: The person is not exist in PhoneHQ table.");
                }
                else if (Operation == OracleDataMaintainKit.Operation.Delete.ToString())
                {
                    //【02-3】刪除台北公司人員資料
                    if (OracleDataMaintainKit.CheckPhoneHQ(PPS_Code))
                    {
                        if (!OracleDataMaintainKit.DeletePhoneHQ(PPS_Code))
                            throw new Exception("Error_05: Delete failed.");
                    }
                    else
                        throw new Exception("Error_05: The person is not exist in PhoneHQ table.");

                }

                //【99】回傳結果
                string ReturnObject = "OK";
                resp.ContentType = "application/json";
                resp.Charset = null;
                fastJSON.JSON.Instance.Parameters.EnableAnonymousTypes = true;
                resultStr = fastJSON.JSON.Instance.ToJSON(ReturnObject);
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }*/
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}